home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / util / libs / graphics3d.lha / src / example / 3dlib.h next >
Encoding:
C/C++ Source or Header  |  1998-10-17  |  11.8 KB  |  458 lines

  1. /*******************************************************
  2.  ** Libreria di routin grafiche per la visualizzazione
  3.  ** in 3d .
  4.  ** Traslata in E V2.1b dalla libreria :
  5.  ** 2B3D Blitz Basic II 3D Graphics Engine Version 0.9 Beta
  6.  ** di Maciej R. Gorny
  7.  **
  8.  ** Autore del porting :
  9.  ** 1997 Patrizio Biancalani
  10.  ** NOTA: 
  11.  ** Nel porting tutte le variabili float sono state mutate
  12.  ** in variabili LONG intese pero' col formato 
  13.  ** a virgola fissa :val.float*FIXV
  14.  ** Questo per 2 motivi:
  15.  ** - E' piu' veloce anche su macchine senza coprocessore
  16.  ** - IL compilatore E V2.1B non tratta direttamente i numeri float
  17.  ** RICORDARSI SEMPRE :
  18.  ** in questa versione dell'E il simbolo
  19.  ** * significa prodotto tra 16bit*16bit=32bit
  20.  ** / significa divisione tra 32bit/16bit=16bit
  21.  ** quindi prestare MOLTA attenzione ai valori che possono
  22.  ** assumere in numeri in virgola fissa quando si usano 
  23.  ** questi valori
  24.  *******************************************************/
  25.  
  26. #define div(a,b) Div(a,b)
  27. #define mul(a,b) Mul(a,b)
  28.  
  29. MODULE 'graphics','graphics/rastport','graphics/clip','layers'
  30. MODULE 'graphics/regions','graphics/gfx','graphics/layers'
  31. MODULE 'intuition/screens','dos/dos','Asl','libraries/Asl'
  32.  
  33. /** 
  34.    moduli per poter usare la nuova libreria graphics3D.library al posto della
  35.    gfx.h
  36. **/
  37. /**
  38.    modules to use the new library graphics3d.library instead of gfx.h
  39. **/
  40. MODULE 'graphics3D','graphics3D_lib'
  41.  
  42. /*** INIZIO CODICI ESEGUIBILI ***/
  43.  
  44. /*** ROUTIN DI USO ANCHE ESTERNO ***/
  45.  
  46. /************************************************
  47.  * apertura e inizializzazione dell'ambiente 3D * 
  48.  * restituisce puntatore ad area generale da    *
  49.  * indicare sempre al richiamo delle altre      *
  50.  * routin.                    *
  51.  ************************************************
  52.  *** INPUT :                    *
  53.  * win  -> puntatore a finestra su cui si vuol  *
  54.  *         operare.                *
  55.  * x0   -> coord. X origine finestra di vis.    *
  56.  * y0   -> coord. Y origine finestra di vis.    *
  57.  * scrw -> larghezza finestra per mondo 3D.     *
  58.  * scrh -> altezza finestra per mondo 3D.       *
  59.  * vdist-> distanza tra osservatore e piano di  *
  60.  *         proiezione.                    *
  61.  *** OUTPUT:                    *
  62.  * puntatore a struttura ambient3d da usarsi in *
  63.  * ingresso di quasi tutte le altre funzioni.   *
  64.  * > 0 allora tutto ok.                *
  65.  * =0 allora errore, operazione fallita.        * 
  66.  ************************************************/
  67. PROC display3d(win:PTR TO window,x0:LONG,y0:LONG,
  68.         scrw:LONG,scrh:LONG,vdist:LONG)
  69. DEF     ris:LONG 
  70.  
  71. ris:=NIL
  72. graphics3dbase:=NIL
  73. IF ((graphics3dbase:=OpenLibrary('graphics3D.library',11))<=NIL) THEN RETURN 0
  74.  
  75. ris:=Gd_display3d(win,x0,y0,scrw,scrh,vdist)
  76.  
  77. WriteF('ris=\d\n',ris)
  78.  
  79. ENDPROC ris
  80.  
  81. /************************************************
  82.  * chiusura ed eliminazione dell'ambiente 3D    * 
  83.  ************************************************
  84.  *** INPUT :                    *
  85.  * in -> valore >0 restituito da display3d.     *
  86.  *** OUTPUT:                    *
  87.  * nessuno.                    *
  88.  ************************************************/
  89. PROC close_display3d(in:LONG)
  90. DEF i:LONG
  91.  
  92. IF in<>NIL THEN Gd_close_display3d(in)
  93.  
  94. IF graphics3dbase>NIL 
  95.     CloseLibrary(graphics3dbase)
  96.     RETURN
  97. ENDIF
  98.  
  99. ENDPROC
  100.  
  101. /************************************************
  102.  ** legge i dati di un oggetto da un file .PLG **
  103.  ** e li inserisce nel mondo 3d come un nuovo  **
  104.  ** oggetto.                       ** 
  105.  ************************************************
  106.  *** INPUT :                        * 
  107.  * in -> valore > 0 restituito da display3d.    *
  108.  * fname -> puntatore a stringa con nome file.  *
  109.  * scalar-> fattore di scala per oggetto.       *
  110.  *          (valore in FIXPOINT).        *
  111.  *** OUTPUT:                    *
  112.  * >0 tutto ok, oggetto letto.            *
  113.  * =0 operazione fallita.            *
  114.  ************************************************/
  115. PROC plgloadobject(in:LONG,fname:PTR TO CHAR,scalar:LONG)
  116. DEF total_vertices:LONG,
  117.     total_polys:LONG,
  118.     num_vertices:LONG,
  119.     color_des:LONG,
  120.     logical_color:LONG,
  121.     shading:LONG,
  122.     index:LONG,
  123.     ln:LONG,
  124.     tempword:LONG,
  125.     p1:LONG,
  126.     p2:LONG,
  127.     p3:LONG,
  128.     p4:LONG,    
  129.     tempbyte:LONG,
  130.     t:LONG,
  131.     k:LONG,
  132.     v:vertex,
  133.     vl:PTR TO LONG,
  134.     temp[500]:ARRAY,
  135.     sbuffer[100]:ARRAY,
  136.     name[50]:ARRAY,
  137.     tempstr[50]:ARRAY,
  138.     file:LONG,
  139.     col:LONG,
  140.     cl:LONG,
  141.     cc:LONG,
  142.     cs:LONG,
  143.     i:LONG,
  144.    esi:LONG
  145.  
  146. esi:=0
  147. file:=Open(fname,OLDFILE)
  148. IF file=NIL THEN RETURN 0
  149. ln:=0
  150. cs:=0
  151. WHILE ((cl:=Read(file,temp,495))>NIL)
  152.     cc:=0
  153.     WHILE (cc<cl)
  154.         sbuffer[cs++]:=temp[cc++]
  155.         IF (sbuffer[cs-1]=$0D) OR (sbuffer[cs-1]=$0A) 
  156.             sbuffer[cs]:=0
  157. /** mettere qui codici di lettura dati da file **/
  158. /** place here the code of reading datas from file **/
  159.     plggetline(sbuffer)
  160. #ifdef DEBUG
  161. WriteF('Line read by plggetline :\s\n',sbuffer)
  162. #endif
  163.     IF StrLen(sbuffer)>3   /* test se linea non vuota *//* test if line not empty */
  164.         INC ln
  165.         IF ln=1        
  166.             tempbyte:=lstr(sbuffer,name,0)        
  167.             tempbyte:=lstr(sbuffer,tempstr,tempbyte)
  168.             total_vertices:=Val(tempstr,{i})
  169. #ifdef DEBUG
  170. WriteF('Total_vertices:\d\n',total_vertices)
  171. #endif
  172.             lstr(sbuffer,tempstr,tempbyte)
  173.             total_polys:=Val(tempstr,{i})
  174. #ifdef DEBUG
  175. WriteF('Total_polys:\d\n',total_polys)
  176. #endif
  177.             t:=Gd_newobj(in,name,total_polys,total_vertices)
  178. #ifdef DEBUG
  179. WriteF('Id. of new object:\d\n',t)
  180. #endif
  181.             IF t=NIL THEN JUMP abort
  182.         /** valori fix point **/
  183.         /** values in fix point **/
  184.         ELSEIF (ln>=2) AND ((total_vertices+1)>=ln)
  185.             tempbyte:=lstr(sbuffer,tempstr,0)
  186.             v.x:=Val(tempstr,{i})
  187.             tempbyte:=lstr(sbuffer,tempstr,tempbyte)
  188.             v.y:=Val(tempstr,{i})
  189.             lstr(sbuffer,tempstr,tempbyte)
  190.             v.z:=Val(tempstr,{i})
  191. #ifdef DEBUG
  192. WriteF('Value of vertex n#\d in integer : x=\d y=\d y=\d\n',ln-2,v.x,v.y,v.z) 
  193. #endif
  194.             v.x:=mul(v.x,scalar)
  195.             v.y:=mul(v.y,scalar)
  196.             v.z:=mul(v.z,scalar)
  197. #ifdef DEBUG
  198. WriteF('Value of vertex n#\d in fixpoint : x=\d y=\d y=\d\n',ln-2,v.x,v.y,v.z) 
  199. WriteF('Result of GD_addobjvertex :\d\n',
  200.             Gd_addobjvertex(in,ln-2,v.x,v.y,v.z) )
  201. #endif
  202. #ifndef DEBUG
  203.             Gd_addobjvertex(in,ln-2,v.x,v.y,v.z)
  204. #endif
  205.         ELSEIF (total_vertices+2<=ln) AND 
  206.             (total_polys+total_vertices+1>=ln)
  207.             tempbyte:=lstr(sbuffer,tempstr,0)
  208.     /** implementati dec. hex($,0x) e binary(%) **/
  209.     /** implemented decimal hex($,0x) and bynary(%) **/
  210.             IF (tempstr[0]="0") AND (tempstr[1]="x") 
  211.                 tempstr[0]:=" "
  212.                 tempstr[1]:="$"
  213.             ENDIF
  214.             color_des:=Val(tempstr,{i})
  215. #ifdef DEBUG
  216. WriteF('Color descriptor (in hexadecimal):\h\n',color_des)
  217. #endif
  218.             tempbyte:=lstr(sbuffer,tempstr,tempbyte)
  219.             tempword:=total_vertices+2
  220.             num_vertices:=Val(tempstr,{i})
  221.             index:=ln-tempword
  222. #ifdef DEBUG
  223. WriteF('Number of vertex in #\d polygons:\d\n',index,num_vertices)
  224. #endif            
  225.             p2:=-1
  226.             p3:=-1
  227.             p4:=-1    
  228.             IF num_vertices>0
  229.                 tempbyte:=lstr(sbuffer,tempstr,tempbyte)
  230.                 p1:=Val(tempstr,{i})
  231. #ifdef DEBUG
  232. WriteF('First vertex n#\d\n',p1)
  233. #endif
  234.             ENDIF
  235.             IF num_vertices>1
  236.                 tempbyte:=lstr(sbuffer,tempstr,tempbyte)
  237.                 p2:=Val(tempstr,{i})
  238. #ifdef DEBUG
  239. WriteF('Second vertex n#\d\n',p2)
  240. #endif
  241.             ENDIF
  242.             IF num_vertices>2
  243.                 tempbyte:=lstr(sbuffer,tempstr,tempbyte)
  244.                 p3:=Val(tempstr,{i})
  245. #ifdef DEBUG
  246. WriteF('Third vertex n#\d\n',p3)
  247. #endif
  248.             ENDIF
  249.             IF num_vertices>3
  250.                 tempbyte:=lstr(sbuffer,tempstr,tempbyte)
  251.                 p4:=Val(tempstr,{i})
  252. #ifdef DEBUG
  253. WriteF('Fourth vertex n#\d\n',p4)
  254. #endif
  255.             ENDIF
  256. #ifdef DEBUG
  257. WriteF('Result of GD_addobjpoly :\d\n',
  258.             Gd_addobjpoly(in,index,p1,p2,p3,p4) )
  259. #endif
  260. #ifndef DEBUG
  261.             Gd_addobjpoly(in,index,p1,p2,p3,p4)
  262. #endif
  263.             col:=And($FF,color_des)
  264.             IF col<16 THEN col:=1
  265.             IF col>16 THEN col:=16
  266. #ifdef DEBUG
  267. WriteF('Color :\d\n',col)
  268. #endif
  269.             Gd_cattpoly(in,index,col,Shr(color_des,12))
  270.         ENDIF
  271.     ENDIF
  272. /****************************************/
  273.             cs:=0
  274.         ENDIF
  275.     ENDWHILE
  276. ENDWHILE
  277. /** 
  278.     remeber that you must be use this function ever than the end of 
  279.     definition of a new object
  280. **/
  281. Gd_recalcobj(in)
  282. esi:=1
  283.  
  284. abort:
  285. Close(file)
  286.  
  287. ENDPROC esi
  288.  
  289. /*** ROUTIN DI USO SOLO INTERNO ***/
  290.  
  291. /******************************************************/
  292. /** elimina i commenti da una stringa di un file PLG **/
  293. /** erase the comment from a string of a PLG file    **/
  294. /******************************************************/
  295. PROC plggetline(strl:PTR TO CHAR)
  296. DEF i:LONG
  297. DEF a:LONG
  298. DEF f:LONG
  299. DEF lens:LONG
  300.  
  301. f:=0
  302. a:=0
  303. lens:=StrLen(strl)
  304. FOR i:=0 TO lens
  305.     IF (strl[i]<>32) THEN f:=1
  306.     IF f<>NIL
  307.         IF (strl[i]="#") OR (strl[i]=";") 
  308.             i:=lens
  309.         ELSE
  310.             strl[a]:=strl[i]
  311.             INC a
  312.         ENDIF
  313.     ENDIF
  314. ENDFOR
  315.  
  316. strl[a]:=0
  317.  
  318. ENDPROC
  319.  
  320. /*************************************************/
  321. /** in str mette la prima sequenza di caratteri **/
  322. /** diversi da spazio che trova in s a partire  **/
  323. /** da start, str deve essere lunga a sufficenza**/
  324. /** ritorno prima pos. dopo sequenza.        **/
  325. /** in 'str' place the first sequence of caracters **/
  326. /** other than space that are in 's' from 'start', **/
  327. /** 'str' must be sufficent long , result is the   **/
  328. /** first position after the sequence.             **/ 
  329. /*************************************************/
  330. PROC lstr(s:PTR TO CHAR,str:PTR TO CHAR,start:LONG)
  331. DEF esi:LONG
  332. DEF f:LONG
  333. DEF ls:LONG
  334. DEF esif:LONG
  335. esi:=0
  336. f:=start
  337. ls:=StrLen(s)
  338.  
  339. /** elimino eventuali spazi precedenti **/
  340. WHILE ((f<ls) AND ((s[f]=" ") OR (s[f]=$0A) OR (s[f]=$0D))) DO INC f
  341. /** calcolo lunghezza sequenza caratteri <> " " **/
  342. esif:=esi+f
  343. WHILE (((esif)<ls) AND (s[esif]<>" ") AND
  344.     (s[esif]<>$0A) AND (s[esif]<>$0D))
  345.     str[esi]:=s[esif] 
  346.     INC esi
  347.     INC esif
  348. ENDWHILE
  349. str[esi]:=0
  350.  
  351. #ifdef DEBUG
  352. WriteF('lstr : s=>\s< str=>\s< start=\d result=\d\n',s,str,start,esi+f)
  353. #endif
  354.  
  355. ENDPROC esi+f
  356.  
  357. /**** FINE ROUTIN ***/
  358.  
  359. /*** ROUTIN DI USO SOLO PER TEST **/
  360.  
  361.  
  362. /*************************************************/
  363. /** stampa contenuto delle strutture polytemp   **/
  364. /*************************************************/
  365. /*
  366. PROC printpolyt(in:LONG)
  367. DEF i:LONG
  368. DEF pt:PTR TO polytemp
  369.  
  370.  
  371. pt:=in.worldpolys
  372. FOR i:=0 TO in.total_polys
  373.     WriteF('pol#\d\n',i)
  374.     WriteF('seg#1 X=\d Y=\d\n',pt[i].x1,pt[i].y1)
  375.     WriteF('seg#2 X=\d Y=\d\n',pt[i].x2,pt[i].y2)
  376.     WriteF('seg#3 X=\d Y=\d\n',pt[i].x3,pt[i].y3)
  377.     WriteF('seg#4 X=\d Y=\d\n',pt[i].x4,pt[i].y4)
  378.     WriteF('seg#5 X=\d Y=\d\n',pt[i].x5,pt[i].y5)
  379.  
  380.     WriteF('n#segmenti=\d col=\d\n',pt[i].numpoints,pt[i].shade)
  381.  
  382.     WriteF('vmode=\d\n',pt[i].vmode)
  383.  
  384. ENDFOR
  385.  
  386. ENDPROC
  387. */
  388.  
  389. /*************************************************/
  390. /** stampa contenuto delle strutture objectnode **/
  391. /*************************************************/
  392. /*
  393. PROC objectprint(in:LONG)
  394. DEF id:LONG
  395. DEF io:PTR TO LONG
  396. DEF i:LONG
  397. DEF k:LONG
  398. DEF vo:PTR TO vertex
  399. DEF vl:PTR TO vertex
  400. DEF vg:PTR TO vertex
  401. DEF vc:PTR TO vertex
  402. DEF obj:PTR TO objectnode
  403. DEF pol:PTR TO polygon
  404.  
  405. WriteF('indici oggetti:\n')
  406. io:=in.iobjects
  407. FOR id:=0 TO in.total_objects-1
  408.     WriteF('#\d oggetto id:\d\n',id,io[id])
  409. ENDFOR
  410. FOR id:=0 TO in.total_objects-1
  411.     in.attuale:=id
  412.     obj:=pobj(in)
  413.     vo:=obj.vorig
  414.     vl:=obj.vlocal
  415.     vc:=obj.vcamera
  416.     pol:=obj.polys
  417.     WriteF('id:\d Name:\s\n',obj.id,obj.name)
  418.     WriteF('num. of vert.:\d\n',obj.numverts)
  419.     WriteF('num. of poly.:\d\n',obj.numpolys)
  420.     WriteF('view_mode:\d\n',obj.shade)
  421.     WriteF('state:\d\n',obj.state)
  422.     WriteF('world pos.: X=\d Y=\d Z=\d\n',
  423.         obj.worldposx/FIXV,obj.worldposy/FIXV,obj.worldposz/FIXV)
  424.     WriteF('bounding box:\n max x:\d y:\d z:\d\n min x:\d y:\d z:\d\n',
  425.         obj.xmax,obj.ymax,obj.zmax,obj.xmin,obj.ymin,obj.zmin)
  426.     WriteF('original vertices:\n')
  427.     FOR k:=0 TO obj.numverts-1
  428.         WriteF('\d \d \d\n',vl[k].x/FIXV,
  429.             vl[k].y/FIXV,vl[k].z/FIXV)
  430.     ENDFOR
  431.     WriteF('local vertices:\n')
  432.     FOR k:=0 TO obj.numverts-1
  433.         WriteF('\d \d \d\n',vl[k].x/FIXV,
  434.             vl[k].y/FIXV,vl[k].z/FIXV)
  435.     ENDFOR
  436.     WriteF('camera vertices:\n')
  437.     FOR k:=0 TO obj.numverts-1
  438.         WriteF('\d \d \d\n',vc[k].x/FIXV,
  439.             vc[k].y/FIXV,vc[k].z/FIXV)
  440.     ENDFOR
  441.     WriteF('poligon list:\n')
  442.     FOR i:=0 TO obj.numpolys-1
  443.         WriteF('poligon #\d\n',i)
  444.         WriteF('\d,',pol[i].vertexlist0)
  445.         WriteF('\d,',pol[i].vertexlist1)
  446.         WriteF('\d,',pol[i].vertexlist2)
  447.         WriteF('\d,',pol[i].vertexlist3)
  448.         WriteF('color:\d shade:\d 2-sided:\d\n',
  449.             pol[i].color,pol[i].shade,pol[i].twosided)
  450.         WriteF('visible:\d clipped:\d active:\d\n',
  451.             pol[i].visible,pol[i].clipped,pol[i].active)
  452.     ENDFOR    
  453. ENDFOR
  454.  
  455. ENDPROC
  456. */
  457.  
  458.